package repo

import (
	"context"
	"ebip/internel/domain"
	"ebip/internel/repository/dao"
	"ebip/internel/repository/dao/user_dao"
	"gorm.io/gorm"
)

// @Description
// @Author 代码小学生王木木

type IUserRepo interface {
	CreateUser(ctx context.Context, user domain.UserInfoDM) (int64, error)
}

type userRepo struct {
	dao user_dao.IUserDAO
}

func NewUserRepo(dao user_dao.IUserDAO) IUserRepo {
	return &userRepo{dao: dao}
}

func (u *userRepo) CreateUser(ctx context.Context, user domain.UserInfoDM) (int64, error) {
	return u.dao.Insert(ctx, u.toEntity(user))
}

func (u *userRepo) toEntity(item domain.UserInfoDM) dao.UserMode {
	return dao.UserMode{
		Model: gorm.Model{
			ID: uint(item.ID),
		},
		Username:    item.Username,
		Phone:       item.Phone,
		Email:       item.Email,
		Description: item.Description,
		Password:    item.Password,
		Nickname:    item.Nickname,
		Gender:      item.Gender,
		Birthday:    item.Birthday,
		IsAdmin:     item.IsAdmin,
		Avatar:      item.Avatar,
		CreateId:    item.CreateId,
	}
}
